import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const devState = {
    userInfo: {
        username: 'dev',
        phoneNumber: 'dev',
        roleList: [
            {
                name:'管理员',
                authoritySet:[
                    'ADMIN'
                ]
            }
        ],
        departmentList: [
            {
                id:'1',
                name: '流调机构1',
                parent:null
            }
        ],
    },
    templateName: '',
    token: 'dev-token',
    login: true,
    loginTime: new Date().getTime(),
    second: 60 * 60 * 24,
    logs: '',
    currentComp: 'main',
    cacheData:{},
    mylogcount:10,
}

const storeToSession = store => {
    // 当 store 初始化后调用
    store.subscribe((mutation, state) => {
      if (sessionStorage.getItem('store')){
          sessionStorage.removeItem('store')
      }
      sessionStorage.setItem('store', JSON.stringify(state))
    })
}

const store = new Vuex.Store({
    state: {
        userInfo:null,
        phone: document.body.clientWidth < 800,
        token: '',
        login:false,
        loginTime:0,
        second: 60 * 60 * 24,
        logs:'',
        currentComp: 'project',
        cacheData:{},
        mylogcount:0,
        projectName: '',
        metaDataName:'',
        roleList:[
            {
                name: "流调人员"
            }
        ],
        authList:[
            {
                name: '超级权限',
                value: 'ADMIN'
            }
        ],
        templateName: '',
        selectedTemp: '',
        metedata: '',
        selectRecord: '',
        reportName: '',
    },
    mutations: {
        reportName(state, o) {
            state.reportName = o;
        },
        selectRecord(state, o) {
            state.selectRecord = o;
        },
        metedata(state, o) {
            state.metedata = o;
        },
        phone(state, o) {
            state.phone = o;
        },
        login(state, o) {
            state.userInfo = o
            state.roleList = o.roleSet
            state.login = true
            state.loginTime = new Date().getTime()
            state.token = o.token
        },
        logout(state)
        {
            state.userInfo=null
            state.login = false
            state.currentComp = 'main'
        },
        currentComp(state, c){
            state.currentComp = c
        },
        mylogcount(state, c){
            state.mylogcount = c
        },
        cache(state, { key, value }) {
        
            const cache = Object.assign(state.cacheData, {})
            cache[key] = value
            state.cacheData = cache
        },
        roleList(state, o) {
            state.roleList = o
        },
        authList(state, o) {
            state.authList = o
        },
        projectName(state, n) {
            state.projectName = n;
        },
        metaDataName(state, n) {
            state.metaDataName = n;
        },
        selectedTemp(state, o) {
            state.selectedTemp = o;
        }
    },
    getters: {
        reportName(state) {
            return state.reportName;
        },
        selectRecord(state) {
            return state.selectRecord;
        },
        metedata(state) {
            return state.metedata;
        },
        selectedTemp(state) {
            return state.selectedTemp;
        },
        phone(state) {
            return state.phone;
        },
        projectName(state) {
            return state.projectName
        },
        metaDataName(state) {
            return state.metaDataName
        },
        isLogin(state) {
            if (state.login )
            {
                if ((new Date().getTime() - state.loginTime) < state.second * 1000 )
                {
                    return true
                }
            }
            return false
        },
        isAdmin(state, getters) {
            if(getters.isLogin && state.userInfo !== null){
                if(state.userInfo.roleSet.findIndex(v => v.name === '管理员') >= 0) {
                    return true
                }
            }
            return false
        },
        isNormal(state, getters)
        {
            if(getters.isLogin && state.userInfo !== null)
            {
                if(state.userInfo.permission == 0x00f)
                {
                    return true
                }
            }
            return false
        },
        username(state, getters) {
            if (getters.isLogin && state.userInfo !== null)
            {
                return state.userInfo.username
            }
            return '未登录'
        },
        isProject(state, getters) {
            if(getters.isAdmin){
                return true;
            }
            if(getters.isLogin && state.userInfo !== null){
                if(state.userInfo.roleSet.findIndex(v => v.name === '项目管理人员') >= 0) {
                    return true
                }
            }
            return false
        },
        isSHRole(state, getters) {
            if(getters.isAdmin){
                return true;
            }
            if(getters.isLogin && state.userInfo !== null){
                if(state.userInfo.roleSet.findIndex(v => v.name === '审核人员') >= 0) {
                    return true
                }
            }
            return false
            // if(getters.isLogin) {
            //     let f = false
            //     for(let i=0; i< state.userInfo.roleList.length; i++) {
            //         if(state.userInfo.roleList[i].findIndex(role => role.name === '审核人员')) {
            //             f = true
            //             break
            //         }
            //         if(state.userInfo.roleList[i].authoritySet.includes('RETRIEVE_RECORD')){
            //             f = true
            //             break
            //         }
            //     }
            //     // let f = state.userInfo.roleList.findIndex( v => v.name === '审核人员')
            //     return f
            // }
            return false
        },
        isLDRole(state, getters) {
            if(getters.isAdmin){
                return true;
            }
            if(getters.isLogin && state.userInfo !== null){
                if(state.userInfo.roleSet.findIndex(v => v.name === '流调人员') >= 0) {
                    return true
                }
            }
            return false
        },
        isManager(state, getters) {
            if(getters.isAdmin ){
                return true;
            }
            if(getters.isLogin && state.userInfo !== null){
                if(state.userInfo.roleSet.findIndex(v => v.name === '管理人员') >= 0) {
                    return true
                }
            }
            return false
        },
        userid(state, getters) {
            if (getters.isLogin && state.userInfo !== null)
            {
                return state.userInfo.phone
            }
        },
        token(state) {
            return state.token
        },
        departmentList(state, getters) {
            if (getters.isLogin)
            {
                return !!(state.userInfo.departmentList && state.userInfo.departmentList.length > 0) ? state.userInfo.departmentList[0].name :'无'
            }
        },
        currentComp(state){
            return  state.currentComp;
        },
        mylogcount(state) {
            return state.mylogcount;
        },
        cache(state) {
            return state.cacheData
        },
        roleList(state) {
            return state.roleList
        },
        authList(state) {
            return state.authList
        },
        authMap(state) {
            const f = {}
            state.authList.forEach(v => f[v.value] = v.name)
            return f
        }
    },
    plugins: [storeToSession]
})

// store.replaceState(devState)
export default store